﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Transitions;

namespace TRTCMain
{
    public partial class StopSharingDesktopForm : Form
    {
        public delegate void BtnClickHandler();
        public event BtnClickHandler BtnClicked;
        private System.Windows.Forms.Timer timer = null;

        public StopSharingDesktopForm()
        {
            InitializeComponent();
            shareButton.MouseMove += StopSharingDesktopForm_MouseMove;
            timer = new System.Windows.Forms.Timer();
            timer.Interval = 500;
            timer.Tick += Timer_Tick;
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            if (!this.ClientRectangle.Contains(this.PointToClient(Cursor.Position)) && this.Top == 0)
            {
                var trans = new Transition(new TransitionType_EaseInEaseOut(1000));
                trans.add(this, "Top", -40);
                trans.run();
            }
        }

        private void StopSharingDesktopForm_Load(object sender, EventArgs e)
        {
            this.Left = (Screen.PrimaryScreen.WorkingArea.Width - this.Width) / 2;
        }

        private void shareButton_CheckStatusChanged(bool isChecked)
        {
            if (!isChecked)
            {
                Hide();
                BtnClicked?.Invoke();
                shareButton.IsChecked = true;
            }
        }

        private void StopSharingDesktopForm_MouseMove(object sender, MouseEventArgs e)
        {
            if (this.Top < 0)
            {
                var trans = new Transition(new TransitionType_EaseInEaseOut(200));
                trans.add(this, "Top", 0);
                trans.run();
            }
        }

        private void StopSharingDesktopForm_Shown(object sender, EventArgs e)
        {
            Task.Factory.StartNew(() =>
            {
                Thread.Sleep(5000);
                if (this.Top == 0)
                {
                    var trans = new Transition(new TransitionType_EaseInEaseOut(1000));
                    trans.add(this, "Top", -40);
                    trans.run();
                    this.Invoke(new Action(() =>
                    {
                        timer.Start();
                    }));                  
                }
            });
        }

        private void StopSharingDesktopForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            timer.Stop();
        }
    }
}
